package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import properties.Constants;
import properties.DefaultProperties;

/**
 * This class provides the setting window for the application.
 * @author Kevin Wagner
 * @version 1.0
 */
public class SettingWindow extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1386179251568845174L;
	/**
	 * 
	 */
	private JPanel contentPane;
	/**
	 * 
	 */
	private DefaultProperties props;
	/**
	 * 
	 */
	private JComboBox langCB;
	/**
	 * 
	 */
	private JButton cancelBtn;
	/**
	 * 
	 */
	private JButton saveBtn;

	/**
	 * Create the settings window.
	 * 
	 * @param props properties
	 */
	public SettingWindow(DefaultProperties props) {
		super(props.getLocalizedText("settings"));
		setAlwaysOnTop(true);
		this.props = props;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 220, 300);
		this.setResizable(false);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JLabel langL = new JLabel(props.getLocalizedText("lang"));

		langCB = new JComboBox();

		saveBtn = new JButton(props.getLocalizedText("save"));
		saveBtn.addActionListener(this);

		cancelBtn = new JButton(props.getLocalizedText("cancel"));
		cancelBtn.addActionListener(this);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
				.setHorizontalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addComponent(
																				langL)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				langCB,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE))
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addComponent(
																				saveBtn)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				cancelBtn)))
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		gl_contentPane
				.setVerticalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(langL)
														.addComponent(
																langCB,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.RELATED,
												219, Short.MAX_VALUE)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(saveBtn)
														.addComponent(cancelBtn))));
		contentPane.setLayout(gl_contentPane);
		loadLangCB();
		this.setVisible(true);
	}

	/**
	 * Loads the data for the language combo box.
	 */
	private void loadLangCB() {
		String selectedLang = props.getProperty("lang");
		String[] langs = Constants.LANGUAGES;
		for (int i = 0; i < langs.length; i++) {
			Locale loc = new Locale(langs[i]);
			langCB.addItem(loc.getDisplayLanguage());
			if (langs[i].equals(selectedLang)) {
				langCB.setSelectedIndex(i);
			}
		}

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(saveBtn)) {
			saveSettings();
		} else if (e.getSource().equals(cancelBtn)) {
			this.dispose();
		}

	}

	/**
	 * Saves the new settings to the property file.
	 */
	private void saveSettings() {
		props.setProperty("lang",
				Constants.LANGUAGES[langCB.getSelectedIndex()]);
		props.saveProperties();
		JOptionPane.showMessageDialog(this,
				props.getLocalizedText("restartMessage"),
				props.getLocalizedText("settingsSaved"),
				JOptionPane.INFORMATION_MESSAGE);
		this.dispose();
	}
}
